<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8"/>
    <title>Phantom · 莫古力拂晓之间</title>
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"/>
    <style>
        :root{
            --bg:#fff;
            --fg:#222;
            --accent:#000;
            --label:#555;
        }
        *{box-sizing:border-box;margin:0;padding:0}
        body{
            font-family:"PingFang SC","Helvetica Neue",Arial,sans-serif;
            background:var(--bg);
            color:var(--fg);
            display:flex;
            align-items:center;
            justify-content:center;
            min-height:100vh;
        }
        .card{
            width:100%;
            min-height:100vh;
            display:flex;
            align-items:center;
            justify-content:center;
            padding:40px 30px;
        }
        .inner{
            width:100%;
            max-width:720px;
            display:flex;
            flex-wrap:wrap;
            gap:32px;
            align-items:flex-start;
        }
        .left{
            flex:1.2 1 0;
        }
        .right{
            flex:1 1 0;
            display:flex;
            align-items:center;
            justify-content:center;
        }
        .right img{
            width:100%;
            aspect-ratio:1/1;
            object-fit:cover;
            border-radius:8px;
        }
        h1{
            font-size:28px;
            color:var(--accent);
            margin-bottom:4px;
            display:flex;
            align-items:center;
            gap:8px;
        }
        .subtitle{font-size:14px;color:var(--label);margin-bottom:22px}
        .row{display:flex;align-items:center;margin-bottom:12px;font-size:15px}
        .row i{width:22px;text-align:center;margin-right:10px;color:var(--accent)}
        .label{color:var(--label);margin-right:6px}
        .value{color:var(--accent);font-weight:600}
        .tags{
            width:100%;
            display:flex;
            flex-wrap:wrap;
            gap:8px;
            margin-top:24px;
            justify-content:center;
        }
        .tag{background:#eee;color:#333;font-size:12px;padding:6px 12px;border-radius:14px}

        /* 手机竖屏改为上下堆叠 */
        @media(max-width:600px){
            .inner{flex-direction:column;}
            .right{order:-1;margin-bottom:24px}
        }

        /* 加载和错误提示样式 */
        .loading, .error {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.9);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            z-index: 1000;
            font-size: 18px;
        }

        .error {
            background: rgba(255, 255, 255, 0.95);
            color: #d32f2f;
        }

        .error button {
            margin-top: 15px;
            padding: 8px 16px;
            background: #f5f5f5;
            border: 1px solid #ccc;
            border-radius: 4px;
            cursor: pointer;
        }

        .hidden {
            display: none;
        }
    </style>
</head>
<body>
<!-- 加载提示 -->
<div id="loading" class="loading">
    <p>正在加载FC信息...</p>
</div>

<!-- 错误提示 -->
<div id="error" class="error hidden">
    <p>无法加载FC信息，将显示默认信息</p>
    <button id="retryBtn">重试</button>
</div>

<!-- 页面内容 -->
<div class="card" id="contentContainer">
    <div class="inner">
        <div class="left">
            <h1>Phantom</h1>
            <div class="subtitle"><虚妄></div>
            <div class="subtitle">
                <i class="fa-solid fa-location-dot"></i>
                莫古力 拂晓之间
            </div>

            <div class="row">
                <i class="fa-regular fa-clock"></i>
                <span class="label">工作日活跃：</span><span class="value" id="weekdayActive">01:00 - 24:00</span>
            </div>
            <div class="row">
                <i class="fa-regular fa-clock"></i>
                <span class="label">周末活跃：</span><span class="value" id="weekendActive">01:00 - 24:00</span>
            </div>
            <div class="row">
                <i class="fa-regular fa-calendar-days"></i>
                <span class="label">成立时间：</span><span class="value" id="createTime">2020-08-17 22:37:01</span>
            </div>
            <div class="row">
                <i class="fa-solid fa-map-location-dot" style="color:#4382e0"></i>
                <span class="label">地址：</span><span class="value">黑衣森林 薰衣草苗圃29区58号</span>
            </div>
            <div class="row">
                <i class="fa-solid fa-users"></i>
                <span class="label">活跃成员：</span><span class="value" id="activeMembers">46/84</span>
            </div>
            <div class="row">
                <i class="fa-solid fa-star"></i>
                <span class="label">部队等级：</span><span class="value" id="guildRank">30</span>
            </div>
            <div class="row">
                <i class="fa-solid fa-shield-halved" style="color:#e6c06b"></i>
                <span class="label">大国防联军：</span><span class="value" id="grandParentName">恒辉队</span>
            </div>
        </div>

        <div class="right">
            <img id="guildPic" src="assets/images/logo - S.jpg" alt="部队徽章">
        </div>

        <div class="tags" id="tagsContainer">
            <span class="tag">欢迎新手</span>
            <span class="tag">欢迎回归者</span>
            <span class="tag">欢迎学生党</span>
            <span class="tag">欢迎工作党</span>
            <span class="tag">硬核玩家</span>
            <span class="tag">休闲玩家</span>
            <span class="tag">挑战极本</span>
            <span class="tag">挑战零式</span>
            <span class="tag">挑战绝本</span>
            <span class="tag">社交活跃</span>
            <span class="tag">演奏</span>
            <span class="tag">挖宝</span>
            <span class="tag">采集制作</span>
            <span class="tag">钓鱼</span>
            <span class="tag">九宫幻卡</span>
            <span class="tag">角色扮演爱好者</span>
            <span class="tag">截图爱好者</span>
            <span class="tag">装修爱好者</span>
            <span class="tag">PVP爱好者</span>
        </div>
    </div>
</div>

<script>
    // API配置
    const API_URL = 'https://phantoms-backend.onrender.com/api/risingstones/guild-info';

    // DOM元素
    const loadingEl = document.getElementById('loading');
    const errorEl = document.getElementById('error');
    const contentContainer = document.getElementById('contentContainer');
    const retryBtn = document.getElementById('retryBtn');

    // 默认数据（页面原始数据）
    const defaultData = {
        guild_name: 'Phantom',
        guild_tag: '虚妄',
        area_name: '莫古力',
        group_name: '拂晓之间',
        active_time_weekday: '01:00-24:00',
        active_time_weekend: '01:00-24:00',
        create_time: '2020-08-17 22:37:01',
        active_member_num: 46,
        member_num: 84,
        guild_rank: '30',
        grand_parentname: '恒辉队',
        guild_pic: 'assets/images/logo - S.jpg',
        guild_label: [
            "欢迎新手",
            "欢迎回归者",
            "欢迎学生党",
            "欢迎工作党",
            "硬核玩家",
            "休闲玩家",
            "挑战极本",
            "挑战零式",
            "挑战绝本",
            "社交活跃",
            "演奏",
            "挖宝",
            "采集制作",
            "钓鱼",
            "九宫幻卡",
            "角色扮演爱好者",
            "截图爱好者",
            "装修爱好者",
            "PVP爱好者"
        ]
    };

    // 初始化页面
    function init() {
        // 首先隐藏内容，显示加载提示
        contentContainer.style.display = 'none';
        loadingEl.classList.remove('hidden');
        errorEl.classList.add('hidden');

        // 尝试从API获取数据
        fetchGuildInfo();
    }

    // 获取部队信息
    async function fetchGuildInfo() {
        try {
            const response = await fetch(API_URL);

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const result = await response.json();

            if (result.code === 10000 && result.data) {
                // API请求成功，使用API数据更新页面
                updatePageWithData(result.data);
            } else {
                // API返回错误代码，使用默认数据
                throw new Error(result.msg || 'API返回错误');
            }
        } catch (error) {
            console.error('获取FC信息失败:', error);
            // 使用默认数据
            updatePageWithData(defaultData);
        }
    }

    // 使用数据更新页面
    function updatePageWithData(data) {
        // 更新基本信息
        document.querySelector('h1').textContent = data.guild_name || defaultData.guild_name;
        document.querySelector('.subtitle').innerHTML = `<${data.guild_tag || defaultData.guild_tag}>`;
        document.querySelector('.subtitle:nth-child(3)').innerHTML =
            `<i class="fa-solid fa-location-dot"></i> ${data.area_name || defaultData.area_name} ${data.group_name || defaultData.group_name}`;

        // 格式化时间显示
        const formatTime = (timeStr) => {
            if (!timeStr) return '未知';
            return timeStr.replace('-', ' - ');
        };

        // 更新详细信息
        document.getElementById('weekdayActive').textContent = formatTime(data.active_time_weekday || defaultData.active_time_weekday);
        document.getElementById('weekendActive').textContent = formatTime(data.active_time_weekend || defaultData.active_time_weekend);
        document.getElementById('createTime').textContent = data.create_time || defaultData.create_time;
        document.getElementById('activeMembers').textContent =
            `${data.active_member_num || defaultData.active_member_num}/${data.member_num || defaultData.member_num}`;
        document.getElementById('guildRank').textContent = data.guild_rank ? data.guild_rank.split('.')[0] : defaultData.guild_rank;
        document.getElementById('grandParentName').textContent = data.grand_parentname || defaultData.grand_parentname;


        // 更新标签
        const tagsContainer = document.getElementById('tagsContainer');
        tagsContainer.innerHTML = ''; // 清空原有标签

        const labels = data.guild_label || defaultData.guild_label;
        labels.forEach(label => {
            const tag = document.createElement('span');
            tag.className = 'tag';
            tag.textContent = label.trim();
            tagsContainer.appendChild(tag);
        });

        // 隐藏加载提示，显示内容
        loadingEl.classList.add('hidden');
        contentContainer.style.display = 'flex';

        // 更新页面高度
        sendHeight();
    }

    // 显示错误提示
    function showError() {
        loadingEl.classList.add('hidden');
        errorEl.classList.remove('hidden');
        contentContainer.style.display = 'none';
    }

    // 重试按钮事件
    retryBtn.addEventListener('click', init);

    // 高度自适应脚本
    function sendHeight() {
        const height = document.body.scrollHeight;
        if (window.parent !== window) {
            window.parent.postMessage({ type: 'resize', height }, '*');
        }
    }

    // 监听DOM变化，保证异步/动态内容也能实时更新
    const mo = new MutationObserver(() => sendHeight());
    mo.observe(document.body, { childList: true, subtree: true, attributes: true });

    // 页面加载完成后初始化
    window.addEventListener('DOMContentLoaded', init);
</script>
</body>
</html>